/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.excesys.exsecs.om.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.RequestExcel;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.om.entity.OmEvenAndFacility;
import com.excesys.exsecs.om.entity.OmInspectionAreaClassification;
import com.excesys.exsecs.om.mapper.OmInspectionAreaClassificationMapper;
import com.excesys.exsecs.om.service.OmEventAndFacilityService;
import com.excesys.exsecs.om.service.OmEventAndRegionService;
import com.excesys.exsecs.om.service.OmInspectionAreaClassificationService;
import com.excesys.exsecs.om.service.OmInspectionItemsService;
import com.excesys.exsecs.om.vo.RegionalClassificationExcelVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 巡查区域分类
 *
 * @author
 * @date 2023-05-19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ominspection")
@Tag(name = "巡查区域分类")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class OmInspectionController {

	private final OmInspectionAreaClassificationService omInspectionAreaClassificationService;

	private final OmInspectionAreaClassificationMapper omInspectionAreaClassificationMapper;

	private final OmEventAndRegionService omEventAndRegionService;

	private final OmEventAndFacilityService omEventAndFacilityService;

	private final OmInspectionItemsService omInspectionItemsService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param omInspectionAreaClassifition 巡检分类表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('om_inspection_area_get')")
	public R getOmInspectionPage(Page page, OmInspectionAreaClassification omInspectionAreaClassifition) {
		List<OmInspectionAreaClassification> list = omInspectionAreaClassificationService
				.getInspectionAreaList(omInspectionAreaClassifition.getName());
		Page pages = omInspectionAreaClassificationService.setPage(list, page);
		return R.ok(pages);
	}

	/**
	 * 分类父类查询
	 * @return
	 */
	@Operation(summary = "分类父类查询", description = "分类父类查询")
	@GetMapping("/getInspectionList")
	@PreAuthorize("@pms.hasPermission('om_inspection_area_get')")
	public R getInspectionList() {
		List<OmInspectionAreaClassification> list = omInspectionAreaClassificationService.getParentList();
		return R.ok(list);
	}

	/**
	 * 分类子类类查询
	 * @return
	 */
	@Operation(summary = "分类子类类查询", description = "分类子类类查询")
	@GetMapping("/getInspectionChlList/{parntId}")
	@PreAuthorize("@pms.hasPermission('om_inspection_area_get')")
	public R getInspectionChlList(@PathVariable("parntId") String parntId) {
		List<OmInspectionAreaClassification> list = omInspectionAreaClassificationService.getChildList(parntId);
		return R.ok(list);
	}

	/**
	 * 通过id查询巡检分类表
	 * @param exId id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{exId}")
	@PreAuthorize("@pms.hasPermission('om_inspection_area_get')")
	public R getById(@PathVariable("exId") Integer exId) {
		return R.ok(omInspectionAreaClassificationService.getById(exId));
	}

	/**
	 * 新增巡检分类表
	 * @param omInspectionAreaClassifition 巡检分类表
	 * @return R
	 */
	@Operation(summary = "新增巡检分类", description = "新增巡检分类")
	@SysLog("新增巡检分类")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('om_inspection_area_add')")
	public R save(@RequestBody OmInspectionAreaClassification omInspectionAreaClassifition) {
		OmInspectionAreaClassification om = omInspectionAreaClassificationService
				.getByName(omInspectionAreaClassifition.getName());
		if (ObjectUtil.isNotEmpty(om)) {
			if (omInspectionAreaClassifition.getName().equals(om.getName())) {
				return R.failed("1", "巡检分类名称存在重复");
			}
		}
		omInspectionAreaClassificationService.save(omInspectionAreaClassifition);

		return R.ok();
	}

	/**
	 * 修改巡检分类表
	 * @param omInspectionAreaClassifition 巡检分类表
	 * @return R
	 */
	@Operation(summary = "修改巡检分类表", description = "修改巡检分类表")
	@SysLog("修改巡检分类表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('om_inspection_area_edit')")
	public R updateById(@RequestBody OmInspectionAreaClassification omInspectionAreaClassifition) {
		OmInspectionAreaClassification om = omInspectionAreaClassificationService
				.getByName(omInspectionAreaClassifition.getName());
		if (ObjectUtil.isNotEmpty(om)) {
			if (omInspectionAreaClassifition.getName().equals(om.getName())) {
				return R.failed("1", "巡检分类名称存在重复");
			}
		}
		return R.ok(omInspectionAreaClassificationService.updateById(omInspectionAreaClassifition));
	}

	/**
	 * 通过id删除巡检异常表
	 * @param exId id
	 * @return R
	 */
	@Operation(summary = "通过id删除巡检分类表", description = "通过id删除巡检分类表")
	@SysLog("通过id删除巡检分类表")
	@DeleteMapping("/{exId}")
	@PreAuthorize("@pms.hasPermission('om_inspection_area_del')")
	public R removeById(@PathVariable Integer exId) {
		OmInspectionAreaClassification om = omInspectionAreaClassificationService.getName(exId.toString());
		// 父类删除,直接删除子项和巡检项
		if (om.getParentId() == 0) {
			List<OmInspectionAreaClassification> list = omInspectionAreaClassificationService
					.getChildList(om.getId().toString());
			// 删除父类巡检项和子类巡检项
			for (OmInspectionAreaClassification classification : list) {
				List<OmEvenAndFacility> omEvenAndFacilityList = omEventAndFacilityService
						.getfacityList(classification.getId().toString());
				for (OmEvenAndFacility omEvenAndFacility : omEvenAndFacilityList) {
					// 删除子类关联巡检项
					omInspectionItemsService.removeById(omEvenAndFacility.getItemsId());
				}
				// 删除子类
				// omEventAndFacilityService.deleteBychlCode(classification.getId().toString());
				// 删除父类
				omEventAndRegionService.deleteAll(classification.getParentId().toString());

				// 删除分类下的子类
				omInspectionAreaClassificationService.removeById(classification.getId());
			}
			// 删除父类
			omInspectionAreaClassificationService.removeById(exId);
		}
		else {
			// 先删除子类巡检项，在删除子类
			// omEventAndFacilityService.deleteBychlCode(om.getId().toString());
			omInspectionAreaClassificationService.removeById(exId);
		}
		return R.ok();
	}

	@Operation(summary = "通过ids批量删", description = "通过ids批量删")
	@SysLog("通过ids删除巡检分类-巡检分类")
	@DeleteMapping("/deleteIds")
	@PreAuthorize("@pms.hasPermission('om_inspection_area_del')")
	public R removeByIds(@RequestBody List<String> ids) {
		for (int i = 0; i < ids.size(); i++) {
			OmInspectionAreaClassification om = omInspectionAreaClassificationService.getName(ids.get(i));
			// 父类删除,直接删除子项和巡检项
			if (ObjectUtils.isEmpty(om)) {
				continue;
			}
			if (om.getParentId() == 0) {
				List<OmInspectionAreaClassification> list = omInspectionAreaClassificationService
						.getChildList(om.getId().toString());
				// 删除父类巡检项和子类巡检项
				for (OmInspectionAreaClassification classification : list) {
					List<OmEvenAndFacility> omEvenAndFacilityList = omEventAndFacilityService
							.getfacityList(classification.getId().toString());
					for (OmEvenAndFacility omEvenAndFacility : omEvenAndFacilityList) {
						// 删除子类关联巡检项
						omInspectionItemsService.removeById(omEvenAndFacility.getItemsId());
					}
					// 删除子类
					// omEventAndFacilityService.deleteBychlCode(classification.getId().toString());
					// 删除父类
					omEventAndRegionService.deleteAll(classification.getParentId().toString());

					// 删除分类下的子类
					omInspectionAreaClassificationService.removeById(classification.getId());
				}
				// 删除父类
				omInspectionAreaClassificationService.removeById(ids.get(i));
			}
			else {
				// 先删除子类巡检项，在删除子类
				// omEventAndFacilityService.deleteBychlCode(om.getId().toString());
				omInspectionAreaClassificationService.removeById(ids.get(i));
			}
		}
		return R.ok();
	}

	/**
	 * 获取分类列表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/getList")
	@PreAuthorize("@pms.hasPermission('om_inspection_area_get')")
	public R getList() {
		return R.ok(omInspectionAreaClassificationService.list());
	}

	/**
	 * 导入点位
	 * @param excelVOList 点位列表
	 * @param bindingResult 错误信息列表
	 * @return ok fail
	 */
	@PostMapping("/import")
	@PreAuthorize("@pms.hasPermission('om_inspection_area_import')")
	public R importRole(@RequestExcel List<RegionalClassificationExcelVo> excelVOList, BindingResult bindingResult) {
		return omInspectionAreaClassificationService.importBamSBuildings(excelVOList, bindingResult);
	}

	/**
	 * 导出excel 表格
	 * @return
	 */

	@Operation(summary = "导出所有数据", description = "导出所有数据")
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('om_inspection_area_export')")
	public List<RegionalClassificationExcelVo> export() {
		return omInspectionAreaClassificationService.listBamSBuildings();
	}

}
